package com.smh.springboottest.config;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shiminghui
 * @date 2024/9/24 14:15
 * @description: TODO
 */
//为了解决多个配置类不在扫描包下,手动导入
public class TestImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        //这里我们是写死的,那么我们如何实现让他动态加载某些配置类

        InputStream is = TestImportSelector.class.getClassLoader().getResourceAsStream("common.imports");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        List<String> list = new ArrayList<>();
        String s = null;
        try {
            while ((s = br.readLine()) != null) {
                list.add(s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return list.toArray(new String[0]);
    }
}
